package com.atom.app.lifecycle

import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.GridLayoutManager
import com.atom.app.lifecycle.base.AppConfig
import com.atom.app.lifecycle.base.AppModel
import com.atom.app.lifecycle.base.AppViewModel
import com.atom.app.lifecycle.base.AppViewModelFactory
import com.atom.app.lifecycle.bean.Record
import com.atom.app.lifecycle.databinding.ActivityMainBinding
import com.atom.app.lifecycle.databinding.CellRecordBinding
import com.atom.app.lifecycle.utils.SimpleAdapter
import com.atom.app.lifecycle.utils.SimpleBindingViewHolder
import com.atom.app.lifecycle.utils.SimpleViewHolder
import com.atom.module.logger.Logger
import com.atom.module.mvvm.base.view.activity.AbstractBaseMVVMActivity
import com.atom.module.mvvm.base.viewmodel.AbstractBaseViewModel
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.GsonUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Date

class RecordActivity : AbstractBaseMVVMActivity<ActivityMainBinding, MainViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_main
    }

    override fun initViewModel(): MainViewModel {
        return providerViewModel(AppViewModelFactory, MainViewModel::class).apply {
            this.initDB(intent.getStringExtra(AppConfig.currentUser)!!)
        }
    }

    override fun initVariableId(): Int {
        return BR.mainViewModel
    }

    override fun initData(view: View) {
        super.initData(view)
        binding.content.layoutManager = GridLayoutManager(this, 10)
        binding.content.adapter = viewModel.adapter
        viewModel.initdate()
    }

    override fun initArgs(intent: Intent, savedInstanceState: Bundle?): Boolean {
        val stringExtra = intent.getStringExtra(AppConfig.currentUser)
        if (stringExtra.isNullOrEmpty()) return false
        return super.initArgs(intent, savedInstanceState)
    }
}

class MainViewModel : AppViewModel<AppModel>(AppModel) {

    val adapter = object : SimpleAdapter<Record.Bean>() {
        override fun getItemViewType(position: Int, data: Record.Bean): Int {
            return R.layout.cell_record
        }

        override fun onCreateViewHolder(
            contentView: View,
            viewType: Int
        ): SimpleViewHolder<Record.Bean> {
            return RecordViewHolder(DataBindingUtil.bind(contentView)!!)
        }
    }

    class RecordViewHolder(binding: CellRecordBinding) :
        SimpleBindingViewHolder<Record.Bean, CellRecordBinding>(binding) {
        override fun onBind(adapter: SimpleAdapter<Record.Bean>, data: Record.Bean) {
            binding.image.background = ColorDrawable().apply {
                this.color = ColorUtils.getRandomColor()
            }
        }
    }

    fun initDB(name: String) {
        model.initAccountDB(name)
        viewModelScope.launch {
            val date = withContext(Dispatchers.IO) {
                model.insertAccountTo(Record.Bean())
            }
        }
    }

    fun initdate() {
        viewModelScope.launch {
            // 根据uuid获取本人的开始时间
            // 根据开始时间去创建结束时间
            val date = withContext(Dispatchers.IO) {
                model.selectAllRecord()
            }
            Logger.e(GsonUtils.toJson(date))
            adapter.addAll(date)
        }
    }
}